﻿@page "/Po/BuyReturn/Approve"
@using nERP.ViewModel.Po.BuyReturnVMs
@using nERP.Model.Po
@using System.ComponentModel.DataAnnotations
@inherits BasePage

<div>
    <ValidateForm @ref="vform" Model="@Model" OnValidSubmit="@Submit">
        <Row ItemsPerRow="ItemsPerRow.Three" RowType="RowType.Normal">
            <Display @bind-Value="@Model.Entity.BuyReturnCode" ShowLabel="true" />
            <Display @bind-Value="@Model.Entity.BuyInWarehouseId" ShowLabel="true" Lookup="AllBuyInWarehouses" />
            <Display @bind-Value="@Model.Entity.BuyId" ShowLabel="true" Lookup="AllBuys" />
            <Display @bind-Value="@Model.Entity.SupplierId" ShowLabel="true" Lookup="AllSuppliers" />
            <Display @bind-Value="@Model.Entity.InvoicesDate" ShowLabel="true" FormatString="yyyy-MM-dd" />
            <Display @bind-Value="@Model.Entity.MakeUserId" ShowLabel="true" Lookup="AllMakeUsers" />
            <Display @bind-Value="@Model.Entity.Status" ShowLabel="true" />
            <Display @bind-Value="@Model.Entity.Remark" ShowLabel="true" />
            <Display @bind-Value="@Model.Entity.TotalPrice" ShowLabel="true" FormatString="0.##" />
            <Display @bind-Value="@Model.Entity.TotalPrice1" ShowLabel="true" FormatString="0.##" />
            <Display @bind-Value="@Model.Entity.TotalPrice2" ShowLabel="true" FormatString="0.##" />
            <Display @bind-Value="@Model.Entity.CreateTime" ShowLabel="true" FormatString="yyyy-MM-dd HH:mm:ss" />
            <Display @bind-Value="@Model.Entity.UpdateTime" ShowLabel="true" FormatString="yyyy-MM-dd HH:mm:ss" />
            <Display @bind-Value="@Model.Entity.CreateBy" ShowLabel="true" />
            <Display @bind-Value="@Model.Entity.UpdateBy" ShowLabel="true" />
            <WTUploadImage @bind-Value="@Model.Entity.InvoicesImage" IsDisabled="true" thumb-width="128" />
        </Row>
        <Divider Icon="fa-fw fa-solid fa-bookmark" Text="@WtmBlazor.Localizer["_Model._BuyReturn._BuyReturnDetail_BuyReturn"]" Alignment="Alignment.Left" />

        <Table TItem="BuyReturnDetail"
               IsPagination="false"
               IsStriped="true" IsBordered="true" IsMultipleSelect="false" @bind-Items="@BuyReturnDetailBuyReturnList2" ShowFooter="true"
               ShowToolbar="false" ShowExtendButtons="false" ShowSkeleton="true">
            <TableColumns>
                <TableColumn @bind-Field="@context.Seq" Text="@WtmBlazor.Localizer["Page.序号"]" />
                <TableColumn @bind-Field="@context.BuyReturnDetailCode" Text="@WtmBlazor.Localizer["Page.采购退货明细单号"]" Visible="false" />
                <TableColumn @bind-Field="@context.GoodsId" Text="@WtmBlazor.Localizer["Page.商品编号"]" />
                <TableColumn @bind-Field="@context.GoodsName" Text="@WtmBlazor.Localizer["Page.商品名称"]" />
                <TableColumn @bind-Field="@context.GoodsSpec" Text="@WtmBlazor.Localizer["Page.商品规格"]" />
                <TableColumn @bind-Field="@context.GoodsUnit" Text="@WtmBlazor.Localizer["Page.商品单位"]" />
                <TableColumn @bind-Field="@context.ReturnNumber" Text="@WtmBlazor.Localizer["Page.退货数量"]" FormatString="0.##" />
                <TableColumn @bind-Field="@context.UnitPrice" Text="@WtmBlazor.Localizer["Page.退货单价"]" FormatString="0.##" />
                <TableColumn @bind-Field="@context.TotalPrice" Text="@WtmBlazor.Localizer["Page.退货金额"]" FormatString="0.##" />
                <TableColumn @bind-Field="@context.DiscountPrice" Text="@WtmBlazor.Localizer["Page.折扣金额"]" FormatString="0.##" />
                <TableColumn @bind-Field="@context.TaxRate" Text="@WtmBlazor.Localizer["Page.税率"]" FormatString="0.##" />
                <TableColumn @bind-Field="@context.TaxUnitPrice" Text="@WtmBlazor.Localizer["Page.含税单价"]" FormatString="0.##" />
                <TableColumn @bind-Field="@context.TaxPrice" Text="@WtmBlazor.Localizer["Page.税额"]" FormatString="0.##" />
                <TableColumn @bind-Field="@context.TotalTaxPrice" Text="@WtmBlazor.Localizer["Page.含税金额"]" FormatString="0.##" />
                <TableColumn @bind-Field="@context.Remark" Text="@WtmBlazor.Localizer["_Admin.Remark"]" />
                <TableColumn @bind-Field="@context.CreateTime" Text="@WtmBlazor.Localizer["_Admin.CreateTime"]" FormatString="yyyy-MM-dd HH:mm:ss" />
                <TableColumn @bind-Field="@context.UpdateTime" Text="@WtmBlazor.Localizer["_Admin.UpdateTime"]" FormatString="yyyy-MM-dd HH:mm:ss" />
                <TableColumn @bind-Field="@context.CreateBy" Text="@WtmBlazor.Localizer["_Admin.CreateBy"]" />
                <TableColumn @bind-Field="@context.UpdateBy" Text="@WtmBlazor.Localizer["_Admin.UpdateBy"]" />
            </TableColumns>

            <FooterTemplate>
                <tr style="background-color:gray; font-weight:600; text-align:center;">
                    <td colspan="5">合计：</td>
                    <td>@sumReturnNumber</td>
                    <td colspan="1" />
                    <td>@sumTotalPrice</td>
                    <td style="color:red;">@sumDiscountPrice</td>
                    <td colspan="2" />
                    <td> @sumTaxPrice</td>
                    <td> @sumTotalTaxPrice</td>
                </tr>
            </FooterTemplate>
        </Table>

        <Row>
            <Textarea @bind-Value="@Model.Remark" ShowLabel="true"></Textarea>
        </Row>
        <div class="modal-footer table-modal-footer">
            <Button ButtonType="ButtonType.Submit" IsAsync="true" OnClick="OnAgree" Text="@WtmBlazor.Localizer["Page.同意"]" />
            <Button ButtonType="ButtonType.Submit" IsAsync="true" OnClick="OnDeny" Color="Color.Danger" Text="@WtmBlazor.Localizer["Page.拒绝"]" />
            <Button OnClick="OnClose" Text="@WtmBlazor.Localizer["Sys.Close"]" />
        </div>

    </ValidateForm>

</div>



@code {
    [Parameter]
    public string id { get; set; }
    [Parameter]
    public string[] ids { get; set; }
    private BuyReturnVM Model = new BuyReturnVM();
    private ValidateForm vform { get; set; }
    private string sumReturnNumber { get; set; }
    private string sumTotalPrice { get; set; }
    private string sumDiscountPrice { get; set; }
    private string sumTaxPrice { get; set; }
    private string sumTotalTaxPrice { get; set; }

    public void OnClose()
    {
        CloseDialog();
    }
    private async Task Submit(EditContext context)
    {

        Model.Entity.BuyReturnDetail_BuyReturn = BuyReturnDetailBuyReturnList2.ToList();

        await PostsForm(vform, "/api/Po/BuyReturn/Approve", (s) => "Sys.OprationSuccess");
    }
    public void OnAgree()
    {
        Model.ActionName = "同意";
        Model.Entity.Status = nERP.Model.StatusTypeEnum.Reviewed;
        Model.Entity.BuyReturnDetail_BuyReturn.ForEach(z0 => z0.Status = nERP.Model.dStatusTypeEnum.Reviewed);
    }
    public void OnDeny()
    {
        Model.ActionName = "拒绝";
    }


    private List<SelectedItem> AllBuyInWarehouses = new List<SelectedItem>();
    private List<SelectedItem> AllBuys = new List<SelectedItem>();
    private List<SelectedItem> AllSuppliers = new List<SelectedItem>();
    private List<SelectedItem> AllMakeUsers = new List<SelectedItem>();
    private IEnumerable<BuyReturnDetail> BuyReturnDetailBuyReturnList2 { get; set; } = new List<BuyReturnDetail>();
    protected override async Task OnInitializedAsync()
    {


        AllBuyInWarehouses = await WtmBlazor.Api.CallItemsApi("/api/Po/BuyReturn/GetBuyInWarehouses");
        AllBuyInWarehouses.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });
        AllBuys = await WtmBlazor.Api.CallItemsApi("/api/Po/BuyReturn/GetBuys");
        AllBuys.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });
        AllSuppliers = await WtmBlazor.Api.CallItemsApi("/api/Po/BuyReturn/GetSuppliers");
        AllSuppliers.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });
        AllMakeUsers = await WtmBlazor.Api.CallItemsApi("/api/Po/BuyReturn/GetFrameworkUsers");
        AllMakeUsers.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });
        if (id != "")
        {
            var rv = await WtmBlazor.Api.CallAPI<BuyReturnVM>($"/api/Po/BuyReturn/{id}");
            Model = rv.Data;
        }
        BuyReturnDetailBuyReturnList2 = Model.Entity.BuyReturnDetail_BuyReturn;
        var Items = Model.Entity.BuyReturnDetail_BuyReturn;
        sumReturnNumber = Items.Any() ? Items.Sum(x => x.ReturnNumber)?.ToString("#0.####") : "0";
        sumTotalPrice = Items.Any() ? Items.Sum(x => x.TotalPrice)?.ToString("#0.####") : "0";
        sumDiscountPrice = Items.Any() ? Items.Sum(x => x.DiscountPrice)?.ToString("#0.####") : "0";
        sumTaxPrice = Items.Any() ? Items.Sum(x => x.TaxPrice)?.ToString("#0.####") : "0";
        sumTotalTaxPrice = Items.Any() ? Items.Sum(x => x.TotalTaxPrice)?.ToString("#0.####") : "0";

        await base.OnInitializedAsync();
    }
}
